<?php
namespace app\agent\controller;
use think\Controller;
use think\Db;
use app\common\controller\AgentBaseController;
use app\shop\model\MemberModel;

class Accounts extends AgentBaseController
{
	//消费会员
	public function promotion_user()
    {
    	$agentid = session('agent_id');
		if(request()->isPost()){
    		
    		$param=input('post.');
			
			if(!empty($param['member_no']))$where['a.member_no'] = ['like','%'.$param['member_no'].'%'];
			
			if(!empty($param['mobile']))$where['a.mobile'] = ['like','%'.$param['mobile'].'%'];
			
			if(!empty($param['begin_time']) && !empty($param['end_time'])){
				$where['a.add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
			}
    	}
		if(input('level_id'))$where['a.level_id'] = input('level_id');
		
		$where['a.agent_id'] = $agentid;
		
    	$list = Db::table('info_member')
				->alias('a')
				->field('a.id,a.level_id,a.add_time,a.member_no,a.member_name,a.mobile,a.score,a.balance,a.state,b.add_time as level_addtime,b.level_name')
				->join('user_level b','a.level_id = b.id')
    			->where($where)
    			->order('a.add_time desc')
    			->paginate(20);
    	$this->assign('list',$list);
		//会员等级
		$user_level = db('user_level')->where('agent_id',$agentid)->order('discount asc')->select();
		$this->assign('user_level',$user_level);
		
    	return $this->fetch();
    }
    //推广会员
	public function consumer_user()
    {
    	$agentid = session('agent_id');
    	$agent_to_user = session('agent_user_id');
		
		$pingci=0;
		$nengli=0;
		if(request()->isPost()){
    		
    		$param=input('post.');
			
			if(!empty($param['mobile']))
			{
				$where['b.mobile'] = ['like','%'.$param['mobile'].'%'];
			}
			if(input("pingci")){//消费频次
				$pingci=input("pingci");
				$pc=db('user_group')->find(input("pinci"));
				$where['b.total_nums']=['BETWEEN',[$pc['nums'],$pc['nums2']]];
			}
			if(input("nengli")){//消费能力
				$nengli=input("nengli");
				$pc=db('user_group')->find(input("nengli"));
				$where['b.total_balance']=['BETWEEN',[$pc['money'],$pc['money2']]];
			}
			if(!empty($param['begin_time']) && !empty($param['end_time'])){
				$where['b.add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
			}
    	}
		$this->assign('pingci',$pingci);
		$this->assign('nengli',$nengli);
		if(input('type_id'))$where['a.trade_id'] = input('type_id');
		
    	$list1 = db('user_group')->where('type',1)->select();
        $this->assign('list1',$list1);
        $list2 = db('user_group')->where('type',2)->select();
        $this->assign('list2',$list2);
		
		$where['b.level'] = $agent_to_user;
		$where['a.operator'] = 2;
		
    	$list = db('log_user_charge')
				->alias('a')
				->field('sum(a.balance) as total_price,count(a.u_id) as total_num,b.id,b.nickname,b.mobile,b.is_verify,b.sex,b.add_time')
				->join('info_user b','a.u_id = b.id','left')
    			->where($where)
				->where('b.mobile','<>','')
    			->group('a.u_id')
    			->order('b.add_time desc')
    			->paginate(10);
    	$this->assign('list',$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
		
    	return $this->fetch();
    }

	//推广会员导出
    public function consumer_dc(){
		$param=input();
		$agent_to_user = session('agent_user_id');
		if(!empty($param['begin_time']) && !empty($param['end_time'])){
			$where['a.add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
		}
		if(!empty($param['mobile']))$where['a.mobile'] = $param['mobile'];
				
		$where['b.level'] = $agent_to_user;
		$where['b.mobile'] = ['neq',''];
		$where['a.operator'] = 2;	
    	$list = db('log_user_charge')
				->alias('a')
				->field('sum(a.balance) as total_price,count(a.u_id) as total_num,b.id,b.nickname,b.mobile,b.is_verify,b.sex,b.add_time')
				->join('info_user b','a.u_id = b.id','left')
    			->where($where)
    			->group('a.u_id')
    			->order('b.add_time desc')
    			->select();
		

		$str ="ID,会员名,手机号,消费次数,消费金额,认证状态,性别,注册时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$id = iconv('utf-8','gbk',$row['id']);
			$nickname = iconv('utf-8','gbk',$row['nickname'].'	');  
			$mobile = iconv('utf-8','gbk',$row['mobile'].'	');
			$total_num = iconv('utf-8','gbk',$row['total_num']);
			$total_price = iconv('utf-8','gbk',$row['total_price']);
			if($row['is_verify']==1){
				$verifyname = '已认证';
			}else{
				$verifyname = '银联';
			}
			$verifyname = iconv('utf-8','gbk',$verifyname); 
			if($row['sex']==1){
				$sexname = '男';
			}else{
				$sexname = '女';
			}
			$sexname = iconv('utf-8','gbk',$sexname); 
			$add_time = iconv('utf-8','gbk',$row['add_time']);
			$str .= $id.",".$nickname.",".$mobile.",".$total_num.",".$total_price.",".$verifyname.",".$sexname.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='推广会员列表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}

    //扫码会员
	public function scancode_user()
    {
    	$agentid = session('agent_id');
    	$agent_to_user = session('agent_user_id');
		
		$pingci=0;
		$nengli=0;
		if(request()->isPost()){
    		
    		$param=input('post.');
		
			if(input("pingci")){//消费频次
				$pingci=input("pingci");
				$pc=db('user_group')->find(input("pinci"));
				$where['b.total_num']=['BETWEEN',[$pc['nums'],$pc['nums2']]];
			}
			if(input("nengli")){//消费能力
				$nengli=input("nengli");
				$pc=db('user_group')->find(input("nengli"));
				$where['b.total_money']=['BETWEEN',[$pc['money'],$pc['money2']]];
			}
			if(!empty($param['begin_time']) && !empty($param['end_time'])){
				$where['a.add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
			}
    	}
		$this->assign('pingci',$pingci);
		$this->assign('nengli',$nengli);
		
    	$list1 = db('user_group')->where('type',1)->select();
        $this->assign('list1',$list1);
        $list2 = db('user_group')->where('type',2)->select();
        $this->assign('list2',$list2);
		
		$where['b.agent_id'] = $agentid;
		//$where['b.mobile']="";
		
		$list = db('info_user')
				->alias('a')
				->join('user_relevance_agent b','a.id = b.user_id','left')
    			->where($where)
    			->order('a.add_time desc')
    			->paginate(10);
    			
    	$this->assign('list',$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
		
    	return $this->fetch();
    }

	//扫码会员导出
    public function scancode_dc(){
		$param=input();
		$agent_to_user = session('agent_user_id');
		if(!empty($param['begin_time']) && !empty($param['end_time'])){
			$where['a.add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
		}

    	$list = db('log_user_charge')
				->alias('a')
				->field('sum(a.balance) as total_price,count(a.u_id) as total_num,b.id,b.nickname,b.mobile,b.is_verify,b.sex,b.add_time')
				->join('info_user b','a.u_id = b.id','left')
    			->where($where)
    			->group('a.u_id')
    			->order('b.add_time desc')
    			->select();
		

		$str ="ID,会员名,手机号,消费次数,消费金额,认证状态,性别,注册时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$id = iconv('utf-8','gbk',$row['id']);
			$nickname = iconv('utf-8','gbk',$row['nickname'].'	');  
			$mobile = iconv('utf-8','gbk',$row['mobile'].'	');
			$total_num = iconv('utf-8','gbk',$row['total_num']);
			$total_price = iconv('utf-8','gbk',$row['total_price']);
			if($row['is_verify']==1){
				$verifyname = '已认证';
			}else{
				$verifyname = '银联';
			}
			$verifyname = iconv('utf-8','gbk',$verifyname); 
			if($row['sex']==1){
				$sexname = '男';
			}else{
				$sexname = '女';
			}
			$sexname = iconv('utf-8','gbk',$sexname); 
			$add_time = iconv('utf-8','gbk',$row['add_time']);
			$str .= $id.",".$nickname.",".$mobile.",".$total_num.",".$total_price.",".$verifyname.",".$sexname.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='扫码会员列表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}


    //会员等级
    public function user_level()
    {
    	$agentid = session('agent_id');
    	$list = Db::table('user_level')->where('agent_id',$agentid)->order('add_time desc')->select();
        $this->assign('list',$list);
    	return $this->fetch();
    }
    public function user_level_add()
    {
    	$agentid = session('agent_id');
    	$oemid = session('oem_id');
    	if($_POST){
            $data['level_name']=$_POST['level_name'];
            $data['discount']=$_POST['discount'];
            $data['agent_id']=$agentid;
            $data['oem_id']=$oemid;
            $data['add_time']=now_time();
            $result=Db::table('user_level')->insert($data);
            if($result){
				return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
    	
    	return $this->fetch();
    }
    public function user_level_update()
    {
    	$agentid = session('agent_id');
    	if($_POST){
           		$data['level_name']=input("level_name");
           		$data['discount']=input("discount");
           	$result=Db::table('user_level')->where('agent_id',$agentid)->update($data);
            if($result){
				return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
        $list = Db::table('user_level')->where('id',$id)->find();
        $this->assign('list',$list);
    	return $this->fetch();
    }
    public function user_level_del()
    {
    	$id = input("id");
    	$result = Db::table('user_level')->where('id',$id)->delete();
        if($result){
			return json(['code'=>200,'msg'=>'成功']);
        }else{
       		return json(['code'=>402,'msg'=>'失败']);
        }
    	
    }
    //会员分组
	public function user_group()
    {
    	$agentid = session('agent_id');
    	$list = Db::table('user_group')->where('type',1)->select();
        $this->assign('list',$list);
        $datd = Db::table('user_group')->where('type',2)->select();
        $this->assign('list1',$datd);
    	return $this->fetch();
    }
    public function fenzu_setting()
    {
    	$agentid = session('agent_id');
    	$id = input('id');
    	if($_POST){
    		if($id){
            	$result=Db::table('user_group')->where('id',$id)->update($data);
            	
    		}else{
            	$result=Db::table('user_group')->insert($data);
    		}
            if($result){
				return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
    	return $this->fetch();
    }
    public function fenzu_setting2()
    {
    	$agentid = session('agent_id');
    	$id = input('id');
    	if($_POST){
    		    $data['group_type']=2;
           		$data['number1']=input("number1");
           		$data['number2']=input("number2");
           		$data['number3']=input("number3");
           		$data['number4']=input("number4");
           		$data['number5']=input("number5");
           		$data['agent_id']=session("agent_id");
           		$data['oem_id']=session("oem_id");
    		if($id){
            	$result=Db::table('user_group')->where('id',$id)->update($data);
            	
    		}else{
            	$result=Db::table('user_group')->insert($data);
    		}
            if($result){
				return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
        $list = Db::table('user_group')->where('agent_id',$agentid)->where('group_type',2)->find();
        $this->assign('list',$list);
    	return $this->fetch();
    }
    
	//添加线下会员
	function member_add(){
		
		$agent_info = db('info_agent')->where('id',session('agent_id'))->find();
		
		
		if(request()->isPost()){
			$param=input('post.');
			
			
			$param['add_time'] 		= now_time();
			$param['oem_id'] 		= $agent_info['oem_id'];
			$param['agency_id1'] 	= $agent_info['agency_id1'];
			$param['agency_id2'] 	= $agent_info['agency_id2'];
			$param['agent_id'] 		= $agent_info['id'];
			
			$member = new MemberModel();
			$res = $member->allowField(true)->save($param);
			
			if($res){
				return json(['code'=>200,'msg'=>'添加成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'添加失败']);
			}
		}
		
		//获取会员等级
		$user_level = db('user_level')->order('discount asc')->select();
		$this->assign('user_level',$user_level);
		
		return $this->fetch();
	}
    
    
    
}
